package com.linln.admin.yearaudit;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linln.common.enums.OrderEnum;
import com.linln.common.exception.order.OrderException;
import com.linln.common.vo.JsonResult;
import com.linln.modules.order.domain.YearauditOrder;
import com.linln.modules.order.service.OrderTakecaruserService;
import com.linln.modules.order.service.YearauditOrderService;
import com.linln.modules.shopsService.service.ShopsService;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author zsc
 * @since 2021-01-06
 * @version 1.0.7
 */
@RequestMapping("/yearaudit/order")
@Controller
public class YearauditOrderController {

	@Autowired
	YearauditOrderService yearauditOrderService;

	@Autowired
	ShopsService shopsService;

	@Autowired
	UserService userService;

	@Autowired
	OrderTakecaruserService orderTakecaruserService;


	@RequiresPermissions("yearaudit:order:index")
	@RequestMapping("/index")
	public String list() {
		return "/yearaudit/order/list";
	}


	@RequiresPermissions("yearaudit:order:list")
	@RequestMapping("/list")
	@ResponseBody
	public JsonResult list(Page<YearauditOrder> page, YearauditOrder yearauditOrder){ // 分页
		//String ss = ShiroUtil.getSubject(); //获取当前用户
		//之查看待取车的
		yearauditOrder.setStatus(OrderEnum.WAITINGCAR.getCode());
		yearauditOrderService.findPickUTheCar(page,yearauditOrder,"");
		return JsonResult.ok().put("data",page);
	}


	@RequiresPermissions("yearaudit:order:edit")
	@RequestMapping("/edit")
	public String edit(String id,String takecaruser, Model model) {
		model.addAttribute("orderId", id);
		model.addAttribute("takecaruser", takecaruser);
		return "/yearaudit/order/edit";
	}


	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("yearaudit:order:edit")
	public JsonResult update(YearauditOrder yearauditOrder) {
		boolean result = yearauditOrderService.updateById(yearauditOrder);
		return result ? JsonResult.ok() : JsonResult.error();
	}

	//获取取车人用户
	@RequiresPermissions("yearaudit:order:takecaruser")
	@RequestMapping("/takecaruserList")
	@ResponseBody
	public JsonResult takecaruser(Page<YearauditOrder> page, User user) { // 分页
		if(Strings.isNotBlank(user.getNickname())){
			user.setNickname("%"+user.getNickname()+"%");
		}
		yearauditOrderService.findUserByRoleId(page,user);
		return JsonResult.ok().put("data",page);
	}

	/**
	 * 指派取车人
	 * @param userId
	 * @param orderId
	 * @return
	 */
	@RequiresPermissions("yearaudit:order:saveOrderTakecaruser")
	@PostMapping("/saveOrderTakecaruser")
	@ResponseBody
	public JsonResult saveOrderTakecaruser(String userId,String orderId){
		try {
			yearauditOrderService.saveOrderTakecaruser(userId,orderId);
		} catch (OrderException e) {
			e.printStackTrace();
			return JsonResult.error(e.getMessage());
		}
		return JsonResult.ok();
	}

}